package com.lattice.exception;

import com.lattice.result.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author mis_wu
 * @date 2022/6/21 20:51
 * @todo 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandle {

    private static final String SERVER_ERROR = "500";
    private static final String SERVER_MSG = "系统异常,请联系管理员!!!";

    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    //exceptionHandler(Exception e,HttpServletResponse response)由于网关无HttpServletResponse,故不返回
    public ResponseResult<Object> exceptionHandler(Exception e){
        e.printStackTrace();
        String msg = SERVER_MSG;
        if (e instanceof BusinessException) {
            msg = e.getMessage();
        }
        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setCode(SERVER_ERROR);
        responseResult.setMsg(msg);
        return responseResult;
    }

}
